<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>menu</title>
    <link rel="stylesheet" href="../../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../../lib/font-awesome-4.7.0/css/font-awesome.min.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <!--搜索开始-->
        <fieldset class="layui-elem-field layuimini-search">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="" id="searchForm">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">关键字</label>
                            <div class="layui-input-inline">
                                <input type="text" name="title" id="search_title" autocomplete="off"
                                       class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <a class="layui-btn" id="data-search-btn"><label
                                    class="layui-icon layui-icon-search"></label>搜索</a>
                            <a class="layui-btn layui-btn-warm" id="data-refresh-btn"><label
                                    class="layui-icon layui-icon-refresh"></label>重置</a>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>
        <!--搜索结束-->

        <!--增加和批量删除-->
        <a class="layui-btn" href="javascript:void(0)" id="addMenuBtn"> <span
                class="layui-icon layui-icon-add-1"></span> 添加</a>
        <!--增加和批量删除-->

        <!--表格开始-->
        <table class="layui-hide" id="dataTable"></table>
        <!--表格结束-->

        <!--表格操作的bar start-->
        <script type="text/html" id="tableBar">
            <a class="layui-btn layui-btn-xs layui-btn-warm data-count-delete" lay-event="update">修改</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>
        <!--表格操作的bar end-->

        <!--添加和修改的弹出层开始-->
        <div id="addOrUpdateDiv" style="display: none;padding: 0.3125rem">
            <form method="post" class="layui-form layui-form-pane" lay-filter="dataForm" id="dataForm">
                <div class="layui-form-item item_leftmenu item_permission">
                    <label class="layui-form-label">父级菜单</label>
                    <div class="layui-input-inline">
                        <input type="hidden" name="pid" id="p_id"/>
                        <input type="text" name="pid_name" id="p_name" readonly="readonly" autocomplete="off"
                               placeholder="请选择父级菜单和权限" class="layui-input">
                    </div>
                    <div class="layui-input-inline">
                        <a class="layui-btn layui-btn-warm" id="doSelectMenu"><label
                                class="layui-icon layui-icon-male"></label>选择</a>
                    </div>
                </div>
                <div class="layui-form-item ">
                    <label class="layui-form-label">名称</label>
                    <div class="layui-input-block">
                        <input type="hidden" name="id"/>
                        <input type="text" name="title" lay-verify="required" autocomplete="off"
                               placeholder="请输入菜单和权限名称" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item item_topmenu  item_leftmenu">
                    <label class="layui-form-label">图标</label>
                    <div class="layui-input-block">
                        <input type="text" name="icon" autocomplete="off" placeholder="请输入菜单和权限地址" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item  item_leftmenu">
                    <label class="layui-form-label">链接地址</label>
                    <div class="layui-input-block">
                        <input type="text" name="href" autocomplete="off" placeholder="请输入菜单和权限地址" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item item_leftmenu">
                    <label class="layui-form-label">TARGET</label>
                    <div class="layui-input-block">
                        <input type="radio" name="target" value="_self" title="_self" checked="">
                        <input type="radio" name="target" value="_blank" title="_blank">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">排序码</label>
                    <div class="layui-input-block">
                        <input type="text" name="sort"  lay-verify="required" autocomplete="off"
                               placeholder="请输入排序码" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block" style="text-align: center;">
                        <a class="layui-btn " lay-submit="" lay-filter="doSubmit"><label
                                class="layui-icon layui-icon-release"></label>提交</a>
                        <a class="layui-btn layui-btn-warm" onclick="javascript:$('#dataForm')[0].reset();"
                           lay-filter="data-search-btn"><label class="layui-icon layui-icon-refresh"></label>重置</a>
                    </div>
                </div>
            </form>
        </div>
        <!--添加和修改的弹出层结束-->
    </div>
</div>


<script src="../../lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
<script src="../../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="../../js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script src="../../lib/common/jquery.cookie.min.js" charset="utf-8"></script>
<script src="../../lib/common/api.js" charset="utf-8"></script>
<script src="../../lib/common/selectLayer.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table', 'layer', 'treeTable', 'laydate'], function () {
        var form = layui.form;
        var table = layui.table;
        var layer = layui.layer;
        var laydate = layui.laydate;
        var treeTable = layui.treeTable;

        var tableIns = treeTable.render({
            tree: {
                iconIndex: 1,    // 折叠图标显示在第几列
                idName: 'id',    // 自定义id字段的名称
                pidName: 'pid',  // 自定义标识是否还有子节点的字段名称
                isPidData: true  // 是否是pid形式数据
            },
            elem: '#dataTable',
            cols: [
                {type: 'numbers'},
                {field: 'title', title: '菜单名称',width:"30%"},
                {field: 'id', title: 'ID', align: "center",width:"0%"},
                {field: 'pid', title: 'PID', align: "center",width:"0%"},
                {field: 'href', title: '链接地址', align: "center",width:"10%",singleLine:true},
                {field: 'target', title: '打开方式', align: "center",width:"10%"},
                {field: 'createTime', title: '创建时间', align: "center",width:"10%"},
                {field: 'sort', title: '排序码', align: "center",width:"10%"},
                {
                    field: 'type', width: '10%', align: 'center', templet: function (d) {
                        if (d.type == 1) {
                            return '<span class="layui-badge layui-bg-gray">按钮</span>';
                        }
                        if (d.type == -1) {
                            return '<span class="layui-badge layui-bg-blue">目录</span>';
                        } else {
                            return '<span class="layui-badge-rim">菜单</span>';
                        }
                    }, title: '类型'
                },
                {title: '操作', templet: '#tableBar',width:"20%", fixed: "right", align: "center"}
            ],
            reqData: function (data, callback) {
                $.ajax({
                    url: menuInfo,
                    type: 'get',
                    success: function (res) {
                        callback(res.data);  // 参数是数组类型
                    },
                    error: function (res) {
                        alert("菜单列表加载错误");
                        console.log(res);
                    }
                });

            }
        });

        //监听行数据的事件
        treeTable.on('tool(dataTable)', function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            if (layEvent == 'delete') {
                $.ajax({
                    url: menuInfo + "/" + data.id,
                    type: 'delete',
                    success: function (res) {
                        layer.msg(res.msg);
                        obj.del();
                        layer.close(index);
                    },
                    error: function (res) {
                        alert("删除错误");
                        console.log("删除错误");
                        console.log(res);
                    }
                });
            } else if (obj.event === 'update') {
                $('#searchForm')[0].reset();
                openUpdate(data);
            }
        });

        // 监听搜索操作
        $("#data-search-btn").on("click", function () {
            var title = $("#search_title").val();
            if (title) {
                tableIns.filterData(title);
            } else {
                tableIns.reload();
            }
        });
        $("#data-refresh-btn").on("click", function () {
            $('#searchForm')[0].reset();
            tableIns.reload();
        });
        //监听添加按钮的事件
        $("#addMenuBtn").click(function () {
            openAdd(); //打开添加的弹出层
        })
        //打开添加公告的弹层
        var mainIndex;
        function openAdd() {
            mainIndex = layer.open({
                type: 1,
                title: '添加菜单和权限',
                content: $("#addOrUpdateDiv"),
                area: ['95%', '95%'],
                success: function (index) {
                    //重置表单
                    $("#dataForm")[0].reset();
                }
            });
        }

        function openUpdate(data) {
            mainIndex = layer.open({
                type: 1,
                title: '修改[' + data.title + ']菜单和权限',
                content: $("#addOrUpdateDiv"),
                area: ['95%', '95%'],
                success: function () {
                    //装载数据表格
                    form.val("dataForm", data);
                    $.get(menuInfo,{id:data.pid},function(res){
                        if(res.data.length>0){
                        $("#p_name").val(res.data[0].title);
                        }else{
                        $("#p_name").val("最顶层节点");
                        }
                    });
                }
            });
        }

        //提交数据
        form.on("submit(doSubmit)", function (data) {
            let params = $("#dataForm").serializeObject();
            $.ajax({
                url: menuInfo,
                data: JSON.stringify(params),
                type: "Post",
                dataType: "json",
                contentType: "application/json;charset=utf-8",
                success: function (data) {
                    if (data.code === 0) {
                        tableIns.reload();
                    } else {
                        layer.msg("保存失败");
                    }
                    layer.close(mainIndex);
                },
                error: function (data) {
                    layer.msg("保存错误"+data);

                }
            });
            return false;
        })


        //监听菜单和权限选择的按钮事件打开菜单和权限选择的弹出层
        $("#doSelectMenu").click(function () {
            var p_id = $("#p_id").val();
            openMenuSelecter(p_id, function (id, name) {
                $("#p_id").val(id);
                $("#p_name").val(name);
            });
        });
    });
</script>
</body>
</html>